package com.hbsx.why.http;

import com.hbsx.why.utils.LogUtils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by liub on 2017/6/26 15:09.
 */

public class HttpAdapter {
    //public static String HEAD_URL = "http://211.149.150.175:8086/xyshop/";
    public static String HEAD_URL = "http://192.168.3.54:8080/xyshop/";
    public static HttpSerives requestSerives, noJsonSerives;
    private static OkHttpClient client = new OkHttpClient();



    public static HttpSerives getSerives() {
        if (requestSerives == null) {
            initHttp();
        }
        return requestSerives;
    }
    public static void initHttp() {
        client = client.newBuilder()
                .addNetworkInterceptor(new HttpInterceptor())
                .connectTimeout(10,TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(10,TimeUnit.SECONDS)//设置读取超时时间
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(HEAD_URL)
                //增加返回值为String的支持
                .addConverterFactory(ScalarsConverterFactory.create())
                //增加返回值为Gson的支持(以实体类返回)
                .addConverterFactory(GsonConverterFactory.create())
                //增加返回值为Oservable<T>的支持
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        requestSerives = retrofit.create(HttpSerives.class);//这里采用的是Java的动态代理模式
    }
//    public static HttpSerives getNoJsonSerives() {
//        if (noJsonSerives == null) {
//            initNoJsonHttp();
//        }
//        return noJsonSerives;
//    }
//
//    public static void initNoJsonHttp() {
//        client = client.newBuilder()
//                .addNetworkInterceptor(new HttpInterceptor())
//                .connectTimeout(10,TimeUnit.SECONDS)
//                .readTimeout(10,TimeUnit.SECONDS)
//                .build();
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(HEAD_URL)
//                //增加返回值为String的支持
//                .addConverterFactory(ScalarsConverterFactory.create())
//                //增加返回值为Gson的支持(以实体类返回)
//                .addConverterFactory(GsonConverterFactory.create())
//                //增加返回值为Oservable<T>的支持
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .client(client)
//                .build();
//        noJsonSerives = retrofit.create(HttpSerives.class);//这里采用的是Java的动态代理模式
//    }

    public static class HttpInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            String url = request.url().toString();
            LogUtils.logi(url);
            return chain.proceed(request);
        }
    }
}
